package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.ShiroUtils;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysWeekNews;
import com.excellent.system.service.ISysWeekNewsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 具体周报Controller
 *
 * @author excellent
 * @date 2021-07-01
 */
@Controller
@RequestMapping("/system/stuweeknews")
public class SysWeekNewsStuController extends BaseController
{
    private String prefix = "system/stuweeknews";

    @Autowired
    private ISysWeekNewsService sysWeekNewsService;

    @GetMapping()
    public String stuweeknews()
    {
        return prefix + "/stuweeknews";
    }

    /**
     * 查询具体周报列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysWeekNews sysWeekNews)
    {
        SysUser user = ShiroUtils.getSysUser();  //拿到当前用户的所有基本信息
        if(user.isAdmin()){   //管理员显示所有周报
            startPage();
            List<SysWeekNews> list = sysWeekNewsService.selectSysWeekNewsList(sysWeekNews);
            Collections.sort(list);
            return getDataTable(list);
        }
        SysWeekNews sysWeekNews1 = new SysWeekNews();
        sysWeekNews.setStuId(Long.parseLong(user.getLoginName())); //将学生的学号转化为长整型
        startPage();
        List<SysWeekNews> list = sysWeekNewsService.selectSysWeekNewsList(sysWeekNews);
        Collections.sort(list);
        return getDataTable(list);
    }

    /**
     * 导出具体周报列表
     */
    @Log(title = "具体周报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysWeekNews sysWeekNews)
    {
        List<SysWeekNews> list = sysWeekNewsService.selectSysWeekNewsList(sysWeekNews);
        ExcelUtil<SysWeekNews> util = new ExcelUtil<SysWeekNews>(SysWeekNews.class);
        return util.exportExcel(list, "stuweeknews");
    }

    /**
     * 新增具体周报
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存具体周报
     */
    @Log(title = "具体周报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysWeekNews sysWeekNews)
    {
        return toAjax(sysWeekNewsService.insertSysWeekNews(sysWeekNews));
    }

    /**
     * 修改具体周报
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        SysWeekNews sysWeekNews = sysWeekNewsService.selectSysWeekNewsById(id);
        mmap.put("sysWeekNews", sysWeekNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存具体周报
     */
    @Log(title = "具体周报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysWeekNews sysWeekNews)
    {
        SysWeekNews sys1 = sysWeekNewsService.selectSysWeekNewsById(sysWeekNews.getId());   //通过id查询到该用户所有信息
        Date date = new Date(System.currentTimeMillis());
        sysWeekNews.setMakeTima(date);  //设置当前提交日期
        Date date1 =  sys1.getDeadTime();  //设置用户的截止日期
        if(date.compareTo(date1)>0){   //判断提交日期和当前日期前后关系，设定
            sysWeekNews.setStatus(2);
        }
        if (date.compareTo(date1)<0){
            sysWeekNews.setStatus(1);
        }
        return toAjax(sysWeekNewsService.updateSysWeekNews(sysWeekNews));
    }

    /**
     * 删除具体周报
     */
    @Log(title = "具体周报", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysWeekNewsService.deleteSysWeekNewsByIds(ids));
    }
}
